package com.woniu.product.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.util.ResponseData;
import com.woniu.product.feign.SkuFeign;
import com.woniu.product.mapper.TSkuCommentMapper;
import com.woniu.product.pojo.TCommentReturn;
import com.woniu.product.pojo.TOrderDetail;
import com.woniu.product.pojo.TSkuComment;
import com.woniu.product.service.TCommentReturnService;
import com.woniu.product.service.TOrderDetailService;
import com.woniu.product.service.TSkuCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author 91211
* @description 针对表【t_sku_comment(商品评价表)】的数据库操作Service实现
* @createDate 2024-05-27 19:27:14
*/
@Service
public class TSkuCommentServiceImpl extends ServiceImpl<TSkuCommentMapper, TSkuComment>
    implements TSkuCommentService{
    @Autowired
    private TOrderDetailService orderDetailService;

    @Autowired
    private TSkuCommentMapper commentMapper;

    @Autowired
    private TCommentReturnService returnService;

    @Autowired
    private SkuFeign skuFeign;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData<?> addComment(TSkuComment comment) {
        //存入评价表
        comment.setCreateTime(new Date());
        save(comment);
        //修改订单详情表
        orderDetailService.lambdaUpdate()
                .setSql("isremark = 1")
                .eq(TOrderDetail::getId,comment.getOrderId())
                .update();
        //修改sku里面好评率
        Long count = lambdaQuery()
                .eq(TSkuComment::getCommentType, "好评")
                .eq(TSkuComment::getSkuId, comment.getSkuId())
                .count();
        Long count1 = lambdaQuery()
                .eq(TSkuComment::getSkuId, comment.getSkuId())
                .count();
        long percentage = Math.round((double) count / count1 * 100);
        skuFeign.updateComment(comment.getSkuId(),String.valueOf(percentage));
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> queryComment(String orderId) {
        TSkuComment comment = lambdaQuery().
                eq(TSkuComment::getOrderId, orderId).one();
        return new ResponseData<>().ok(comment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData<?> updateComment(TSkuComment comment) {
        lambdaUpdate().setSql("comment_type = '" + comment.getCommentType() + "'")
                .setSql("remark = '" + comment.getRemark() + "'")
                .setSql("update_time = now()")
                .eq(TSkuComment::getOrderId,comment.getOrderId())
                .update();
        //修改sku里面好评率
        Long count = lambdaQuery()
                .eq(TSkuComment::getCommentType, "好评")
                .eq(TSkuComment::getSkuId, comment.getSkuId())
                .count();
        Long count1 = lambdaQuery()
                .eq(TSkuComment::getSkuId, comment.getSkuId())
                .count();
        long percentage = Math.round((double) count / count1 * 100);
        skuFeign.updateComment(comment.getSkuId(),String.valueOf(percentage));
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> queryCommentByUserId(Long userId) {
        List<TSkuComment> tSkuComments = commentMapper.queryCommentByUserId(userId);
        return new ResponseData<>().ok(tSkuComments);
    }

    @Override
    public ResponseData<?> deleteShopComment(Long returnId) {
        returnService.lambdaUpdate()
                .set(TCommentReturn::getStatus, 0)
                .eq(TCommentReturn::getId, returnId)
                .update();
        return new ResponseData<>().ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData<?> deleteMyComment(TSkuComment comment) {
        commentMapper.deleteById(comment.getId());
        //删除下面回复
        returnService.lambdaUpdate()
                .set(TCommentReturn::getStatus, 0)
                .eq(TCommentReturn::getId, comment.getReturnId())
                .update();
        //修改sku里面好评率
        Long count = lambdaQuery()
                .eq(TSkuComment::getCommentType, "好评")
                .eq(TSkuComment::getSkuId, comment.getSkuId())
                .count();
        Long count1 = lambdaQuery()
                .eq(TSkuComment::getSkuId, comment.getSkuId())
                .count();
        long percentage = Math.round((double) count / count1 * 100);
        skuFeign.updateComment(comment.getSkuId(),String.valueOf(percentage));
        //修改订单详情表
        orderDetailService.lambdaUpdate()
                .setSql("isremark = 0")
                .eq(TOrderDetail::getId,comment.getOrderId())
                .update();
        return new ResponseData<>().ok();
    }
}




